package flexulous.view;


//{{{ Imports

import javax.swing.border.EmptyBorder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.util.Arrays;

import org.gjt.sp.jedit.buffer.FoldHandler;
import org.gjt.sp.jedit.buffer.JEditBuffer;
import org.gjt.sp.jedit.options.BufferOptionPane;

import org.gjt.sp.jedit.*;
import org.gjt.sp.jedit.gui.*;
import flexulous.model.*;
import flexulous.util.*;
import flexulous.*;


//}}}

/**
 * Flexulous target options dialog.
 * 
 */

public class FlexulousTargetEditor extends EnhancedDialog
{
	private FlexulousCompilerHelper helper;
    private FlexulousTarget target;
	private boolean isEditing;
	private View view;
	private FlexulousTargetEditorPane panel;
	private JButton ok;
	private JButton cancel;
    private JButton delete;
	
	public FlexulousTargetEditor(View view, FlexulousCompilerHelper helper)
	{
		super(view,jEdit.getProperty("editor.flexulous.titleAdd"),true);
		this.view = view;
		this.helper = helper;
        this.target = helper.getTarget();
		if(target.getName() == ""){
			isEditing = false; //We're adding.
		}else{
			isEditing = true;
			setTitle(jEdit.getProperty("editor.flexulous.titleEdit"));
		}
		JPanel content = new JPanel(new BorderLayout());
		content.setBorder(new EmptyBorder(12,12,12,12));
		setContentPane(content);

		ActionHandler actionListener = new ActionHandler();
		panel = new FlexulousTargetEditorPane(target);

		content.add(BorderLayout.NORTH,panel);

		//{{{ Buttons

		JPanel buttons = new JPanel();
		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
		buttons.setBorder(new EmptyBorder(12,0,0,0));
		buttons.add(Box.createGlue());
		
		ok = new JButton(jEdit.getProperty("editor.flexulous.add"));
		ok.addActionListener(actionListener);
		if(isEditing){
			ok.setLabel(jEdit.getProperty("editor.flexulous.save"));
		}

		getRootPane().setDefaultButton(ok);
		buttons.add(ok);

		//buttons.add(Box.createHorizontalStrut(6));
        
        if (isEditing) {
            delete = new JButton(jEdit.getProperty("editor.flexulous.delete"));
            delete.addActionListener(actionListener);
            buttons.add(delete);
        }

		cancel = new JButton(jEdit.getProperty("common.cancel"));
		cancel.addActionListener(actionListener);
		buttons.add(cancel);

		buttons.add(Box.createGlue());
		content.add(BorderLayout.SOUTH,buttons);

		//}}}

		pack();
		setLocationRelativeTo(view);
		setVisible(true);
	} //}}}



	//{{{ ok() method

	public void ok()
	{
		if(panel.validateTarget())
		{
			panel.save();
			if(!isEditing){
				//add target.
				 FlexulousPlugin.getInstance().getModel().addHelper(helper);
			}
			dispose();
		}
	} //}}}

	//{{{ cancel() method

	public void cancel()
	{
		dispose();
	} //}}}
    
    public void delete() {
        FlexulousPlugin.getInstance().getModel().removeHelper(helper);
        dispose();
    }

	//{{{ ActionHandler class

	class ActionHandler implements ActionListener
	{
		//{{{ actionPerformed() method
		public void actionPerformed(ActionEvent evt)
		{
			Object source = evt.getSource();
			if(source == ok)
				ok();
			else if(source == cancel)
				cancel();
            else if (source == delete)
                delete();
		} //}}}

	} //}}}

	//}}}
}

